import clouds1 from './clouds1.png'
import clouds2 from './clouds2.png'

import gsap from "gsap";

class clouds {
    constructor(map, options) {
        if (!map) throw new Error('no viewer object!');
        this.map = map;
        this.viewer = map.getViewer();
        options = options || {};
        this.init();
    }
    init() {
        this._$rectangle = this.viewer.scene.primitives.add(
            new Cesium.Primitive({
                geometryInstances: new Cesium.GeometryInstance({
                    geometry: new Cesium.RectangleGeometry({
                        rectangle: Cesium.Rectangle.fromDegrees(-180.0, -89.5, 180.0, 89.5),
                        height: 300000
                    }),
                }),
                appearance: new Cesium.EllipsoidSurfaceAppearance({
                    aboveGround: true,
                    material: new Cesium.Material({
                        fabric: {
                            type: "Image",
                            uniforms: {
                                image: clouds1,
                                alpha: 0.7 //透明度
                            },
                            components: {
                                diffuse: 'texture2D(image, fract(repeat * materialInput.st)).rgb',
                                alpha: 'texture2D(image, fract(repeat * materialInput.st)).a * alpha'//设置透明度不可或缺的一句
                            }
                        },
                        translucent: true
                    })
                }),
            })
        );
        let rotationM = Cesium.Matrix3.fromRotationZ(Cesium.Math.toRadians(0.01));
        this._update = () => {
            if (this._$rectangle) {
                let newMatrix4 = Cesium.Matrix4.multiplyByMatrix3(this._$rectangle.modelMatrix, rotationM, new Cesium.Matrix4());// 计算矩阵4的变换矩阵（在原变换中，累加变换）
                this._$rectangle.modelMatrix = newMatrix4;
            }
        }
        this.map.on('Tick', this._update)
    }
    destroy() {
        if (!this.viewer || !this._$rectangle) return;
        this.viewer.scene.primitives.remove(this._$rectangle);
        this.map.off('Tick', this._update)
    }
}

Cesium.clouds = clouds;